//------------------------------------------------------------------------------
//	file midi_ports.c
//	brief
//		Display all midi port information
//
//	$Id: midi_ports.c 276 2014-04-17 07:34:45Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2014 A.C. Verbeck
//
//	This file is part of winMIDI: windows MIDI -- a direct MIDI interface
//
//	winMIDI is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	winMIDI is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with winMIDI.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#include <stdint.h>																//	Standard int types
#include <stdbool.h>															//	Bool

#include <popt.h>
#include <Windows.h>
#include <mmsystem.h>

#include "bi.h"

//------------------------------------------------------------------------------
//  Display information on all MIDI input devices
//
//  szPname:
//      Product name in a null-terminated string.
//
//  wMid:
//      Manufacturer identifier of the device driver for the MIDI output device.
//      Manufacturer identifiers are defined in Manufacturer and Product Identifiers.
//
//  wPid:
//      Product identifier of the MIDI output device.
//      Product identifiers are defined in Manufacturer and Product Identifiers.
//
//  vDriverVersion:
//      Version number of the device driver for the MIDI output device.
//      The high-order byte is the major version number,
//      the low-order byte is the minor version number.
//
//------------------------------------------------------------------------------
static void dispMidiInDevs(void)
{
	UINT nMidiDeviceNum;
	MIDIINCAPS caps;

	nMidiDeviceNum = midiInGetNumDevs();
	if (nMidiDeviceNum == 0) {
		printf("No MIDI input ports\n");
		return;
	}
	printf("MIDI input ports:\n");
	printf(" %s %-32s %-4s %-4s %-4s\n","id","name","man","prod","ver");
	for (unsigned int i = 0; i < nMidiDeviceNum; ++i) {
		midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
		printf("  %d %-32s ", i, caps.szPname);
		printf("%04x %04x %04x", caps.wMid, caps.wPid, caps.vDriverVersion);
		printf("\n");
	}
	printf("\n");
}

//------------------------------------------------------------------------------
//  Display information on all MIDI output devices
//
//  szPname:
//      Product name in a null-terminated string.
//
//  wMid:
//      Manufacturer identifier of the device driver for the MIDI output device.
//      Manufacturer identifiers are defined in Manufacturer and Product Identifiers.
//
//  wPid:
//      Product identifier of the MIDI output device.
//      Product identifiers are defined in Manufacturer and Product Identifiers.
//
//  vDriverVersion:
//      Version number of the device driver for the MIDI output device.
//      The high-order byte is the major version number,
//      the low-order byte is the minor version number.
//
//  wTechnology:
//      Type of the MIDI output device (enum)
//
//  Voices:
//      Number of voices supported by an internal synthesizer device.
//      If the device is a port, this member is not meaningful and is set to 0.
//
//  wNotes:
//      Maximum number of simultaneous notes that can be played
//      by an internal synthesizer device.
//      If the device is a port, this member is not meaningful and is set to 0.
//
//  wChannelMask:
//      Channels that an internal synthesizer device responds to,
//      where the least significant bit refers to channel 0
//      and the most significant bit to channel 15.
//      Port devices that transmit on all channels set this member to 0xFFFF.
//
//------------------------------------------------------------------------------
static void dispMidiOutDevs(void)
{
	UINT nMidiDeviceNum;
	MIDIOUTCAPS caps;

	nMidiDeviceNum = midiOutGetNumDevs();
	if (nMidiDeviceNum == 0) {
		printf("No MIDI output ports\n");
		return;
	}
	printf("MIDI output ports:\n");
	printf(" %s %-32s %-4s %-4s %-4s %-4s %-4s %-4s %-4s\n","id","name","man","prod","ver","tech","vocs","note", "mask");
	for (unsigned int i = 0; i < nMidiDeviceNum; ++i) {
		midiOutGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
		printf("  %d %-32s ", i, caps.szPname);
		printf("%04x %04x %04x ", caps.wMid, caps.wPid, caps.vDriverVersion);
		printf("%04x %04x %04x %04x", caps.wTechnology, caps.wVoices, caps.wNotes, caps.wChannelMask);
		printf("\n");
	}
	printf("\n");
}

//------------------------------------------------------------------------------
//  Main application
//  - Print information on the MIDI input ports
//  - Print information on the MIDI output ports
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	dispMidiInDevs();
	dispMidiOutDevs();
	return 0;
}

//
//  End: midi_ports.c
//
